package com.aistar.service.impl;

import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.CustomerExample;
import com.aistar.service.CustomerService;
import com.aistar.util.MyBatisUtil;
import com.aistar.util.ServerResponse;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CustomerServiceImpl implements CustomerService {

    //根据账号与密码查询用户
    public ServerResponse getByAccountAndPwd(Integer type,String account,String password){
        SqlSession session = MyBatisUtil.getSqlSession();
        CustomerMapper mapper = session.getMapper(CustomerMapper.class);

        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria =  example.createCriteria();

        switch (type){
            case 0:  {
                Long telno =Long.valueOf(account);
                criteria.andCustTelnoEqualTo(telno);
                break;
            }
            case 1:
                criteria.andCustEmailEqualTo(account);
                break;
            case 2:
                criteria.andCustNameEqualTo(account);

        }

        criteria.andCustPasswordEqualTo(password);
        List<Customer> customerList = mapper.selectByExample(example);
        MyBatisUtil.closeSqlSession();


        if(customerList!=null && customerList.size()>0)
            return ServerResponse.getDataSuccess("登录成功");

        return ServerResponse.getDataFailed("账号或密码错误，登录失败");
    }



    @Override
    public ServerResponse getByTelno(Long telno) {
        SqlSession session = MyBatisUtil.getSqlSession();
       CustomerMapper mapper =  session.getMapper(CustomerMapper.class);



       CustomerExample example = new CustomerExample();
       example.createCriteria().andCustTelnoEqualTo(telno);
       List<Customer> customerList = mapper.selectByExample(example);
       MyBatisUtil.closeSqlSession();


       if(customerList!=null && customerList.size()>0){//找到用户了
           Customer customer = customerList.get(0);
           return ServerResponse.hasRegist(); // 1,"查询数据成功",customer
       }else{// 用户不存在
           return ServerResponse.noRegist(); // 0,"查询数据失败","用户不存在"
       }


    }

    @Override
    public ServerResponse save(Customer customer) {
        SqlSession session = MyBatisUtil.getSqlSession();
        CustomerMapper mapper =  session.getMapper(CustomerMapper.class);
        int rows = mapper.insert(customer);
        session.commit();
        MyBatisUtil.closeSqlSession();
        if(rows == 0)
            return ServerResponse.addFailed();

        return ServerResponse.addSuccess();
    }

    @Override
    public ServerResponse modifyStatus(Customer customer) {
        return null;
    }

    @Override
    public ServerResponse modify(Customer customer) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
        return null;
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }
}
